<script setup>
import { ref } from 'vue'
const dialogVisible = ref(false)
const isabled = ref()
isabled
const formModel = ref({
  id: '',
  vegetableid: '',
  seedcosts: '',
  fertilizercosts: '',
  pesticidecosts: '',
  laborcosts: '',
  othercosts: '',
  totalcosts: '',
  remarks: '',
  name: '',
  userid: '',
  tar: ''
})

const rules = {
  seedcosts: [
    { required: true, message: '至少是 0', trigger: 'blur' },
    {
      pattern: /^\d+(\.\d{1,2})?$/,
      message: '请输入正确格式的金额，最多保留两位小数',
      trigger: 'blur'
    }
  ],
  name: [
    { required: true, message: '请输入菜地名称', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 40 个字符', trigger: 'blur' }
  ],
  fertilizercosts: [
    { required: true, message: '至少是 0', trigger: 'blur' },
    {
      pattern: /^\d+(\.\d{1,2})?$/,
      message: '请输入正确格式的金额，最多保留两位小数',
      trigger: 'blur'
    }
  ],
  pesticidecosts: [
    { required: true, message: '至少是 0', trigger: 'blur' },
    {
      pattern: /^\d+(\.\d{1,2})?$/,
      message: '请输入正确格式的金额，最多保留两位小数',
      trigger: 'blur'
    }
  ],
  laborcosts: [
    { required: true, message: '至少是 0', trigger: 'blur' },
    {
      pattern: /^\d+(\.\d{1,2})?$/,
      message: '请输入正确格式的金额，最多保留两位小数',
      trigger: 'blur'
    }
  ],
  othercosts: [
    { required: true, message: '至少是 0', trigger: 'blur' },
    {
      pattern: /^\d+(\.\d{1,2})?$/,
      message: '请输入正确格式的金额，最多保留两位小数',
      trigger: 'blur'
    }
  ],
  totalcosts: [
    { required: true, message: '至少是 0', trigger: 'blur' },
    {
      pattern: /^\d+(\.\d{1,2})?$/,
      message: '请输入正确格式的金额，最多保留两位小数',
      trigger: 'blur'
    }
  ]
}
const form = ref()
const disabled = ref(false)
const emit = defineEmits(['success'])
import {
  userplantingcostinfoAddService,
  userplantingcostinfoUpService
} from '@/api/user/plantingcost'
import { useUserStore } from '@/stores'

const sureQuire = async () => {
  formModel.value.tar = '1'
  await form.value.validate()
  const isEdit = formModel.value.id
  formModel.value.userid = `${useUserStore().user.id}`
  if (isEdit) {
    userplantingcostinfoUpService(formModel.value)
      .then((response) => {
        if (response.data.status === 200) {
          ElMessage.success(response.data.message)
          dialogVisible.value = false
          emit('success')
        } else {
          dialogVisible.value = false
          ElMessage.error(response.data.message)
        }
      })
      .catch((error) => {
        ElMessage.error(error)
      })
  } else {
    userplantingcostinfoAddService(formModel.value)
      .then((response) => {
        if (response.data.status == '200') {
          dialogVisible.value = false
          emit('success')
          ElMessage.success(response.data.message)
        } else {
          dialogVisible.value = false
          ElMessage.error(response.data.message)
        }
      })
      .catch((error) => {
        ElMessage.error(error)
      })
  }
}
const NoQuire = () => {
  dialogVisible.value = false
  form.value.resetFields()
}
const handleClose = () => {
  dialogVisible.value = false
  form.value.resetFields()
}
const handleClearValidate = () => {}
//
const open = (row) => {
  dialogVisible.value = true
  formModel.value = { ...row } //添加的话，重置表单内容
  if (!formModel.value.id) {
    disabled.value = false
    formModel.value.seedcosts = 0
    formModel.value.fertilizercosts = 0
    formModel.value.pesticidecosts = 0
    formModel.value.laborcosts = 0
    formModel.value.othercosts = 0
    formModel.value.totalcosts = 0
  } else {
    disabled.value = true
  }
}
// 处理日期变化事件
defineExpose({
  open
})
const handleInput = () => {
  const total =
    parseFloat(formModel.value.seedcosts) +
    parseFloat(formModel.value.fertilizercosts) +
    parseFloat(formModel.value.pesticidecosts) +
    parseFloat(formModel.value.laborcosts) +
    parseFloat(formModel.value.othercosts)
  formModel.value.totalcosts = total.toFixed(2)
}
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    :title="formModel.id ? '修改' : '添加'"
    width="550"
    :before-close="handleClose"
  >
    <el-form
      class="el-form"
      :rules="rules"
      ref="form"
      :model="formModel"
      :before-close="handleClearValidate"
    >
      <el-form-item prop="name" label="菜地名称" required="true">
        <el-input
          v-model="formModel.name"
          :prefix-icon="User"
          placeholder="请输入"
          style="width: 450px"
          :disabled="disabled"
        ></el-input>
      </el-form-item>
      <el-form-item prop="seedcosts" label="种子费用">
        <el-input
          v-model="formModel.seedcosts"
          min="0"
          type="number"
          style="width: 90px"
          @input="handleInput"
        />
      </el-form-item>
      <el-form-item prop="fertilizercosts" label="肥料费用">
        <el-input
          v-model="formModel.fertilizercosts"
          min="0"
          type="number"
          style="width: 90px"
          @input="handleInput"
        />
      </el-form-item>
      <el-form-item prop="pesticidecosts" label="农药费用">
        <el-input
          v-model="formModel.pesticidecosts"
          min="0"
          type="number"
          style="width: 90px"
          @input="handleInput"
        />
      </el-form-item>
      <el-form-item prop="laborcosts" label="工人费用">
        <el-input
          v-model="formModel.laborcosts"
          min="0"
          type="number"
          style="width: 90px"
          @input="handleInput"
        />
      </el-form-item>
      <el-form-item prop="othercosts" label="其他费用">
        <el-input
          v-model="formModel.othercosts"
          min="0"
          type="number"
          style="width: 90px"
          @input="handleInput"
        />
      </el-form-item>
      <el-form-item prop="totalcosts" label="总花费用">
        <el-input
          v-model="formModel.totalcosts"
          min="0"
          type="number"
          style="width: 90px"
          :disabled="true"
        />
      </el-form-item>
      <el-form-item
        prop="remarks"
        label="备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注"
      >
        <el-input
          v-model="formModel.remarks"
          :prefix-icon="User"
          :autosize="{ minRows: 2, maxRows: 4 }"
          style="width: 450px"
          type="textarea"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="NoQuire">取消</el-button>
        <el-button type="primary" @click="sureQuire"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<style>
.el-form {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
}
</style>
